import { ComponentSlice_StateType, QuestionInfoType } from "../store/ComponentReducer"
const key = 'USERTOKEN'
export function GetUserToken() {
    return localStorage.getItem(key)
}

export function SetUserToken(val: string) {
    localStorage.setItem(key, val)
}

export function ClearUserToken() {
    localStorage.removeItem(key)
}



export const getNextSelectedId = (fe_id: string, componentList: QuestionInfoType[]) => {
    const filterComponentlist = componentList.filter((c) => !c.hide)
    const index = filterComponentlist.findIndex((c) => c.fe_id === fe_id)
    if (index === -1) {
        return ''
    }
    if (filterComponentlist.length <= 1) {
        return ''
    } else {
        if (index + 1 === filterComponentlist.length) {
            return filterComponentlist[index - 1].fe_id
        } else if (index < filterComponentlist.length) {
            return filterComponentlist[index + 1].fe_id
        }
    }
}

export const addNewComponent = (draft: ComponentSlice_StateType, newComponent: QuestionInfoType) => {
    const { selectedId, componentList } = draft
    if (selectedId !== '') {
        const index = componentList.findIndex((c) => c.fe_id === selectedId)
        componentList.splice(index + 1, 0, newComponent)
    } else {
        draft.componentList.push(newComponent)
    }
    draft.selectedId = newComponent.fe_id
}